﻿using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;

namespace Utilities.Xml
{
   public static class XmlDataContractSerializer
   {
       public static string BuildCleanXML<T>(T data) where T : new()
       {
           if (data == null)
           {
               throw new ArgumentException("The model object cannot be empty", "model");
           }

           try
           {
               XmlWriterSettings writerSettings = new XmlWriterSettings();
               XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
               namespaces.Add(string.Empty, string.Empty);
               writerSettings.OmitXmlDeclaration = true;
               using (StringWriter stringWriter = new StringWriter())
               {
                   XmlSerializer xs = new XmlSerializer(data.GetType());
                   using (XmlWriter writer = XmlWriter.Create(stringWriter, writerSettings))
                   {
                       xs.Serialize(writer, data, namespaces);
                       return stringWriter.ToString();
                   }
               }
           }
           catch { throw; }
       }



       private static string SerializeData(object data)
       {
           if (data != null)
           {
               string stringData = data as string;
               if (stringData != null)
               {
                   return stringData;
               }
               else
               {
                   try
                   {
                       String XmlizedString = null;
                       using (MemoryStream memoryStream = new MemoryStream())
                       {
                           XmlSerializer xs = new XmlSerializer(data.GetType());
                           XmlWriterSettings writerSettings = new XmlWriterSettings();
                           writerSettings.Encoding = new System.Text.UnicodeEncoding();
                           using (XmlWriter xmlTextWriter = XmlWriter.Create((Stream)memoryStream, writerSettings))
                           {
                               xs.Serialize(xmlTextWriter, data);

                               string thestring = xmlTextWriter.ToString();
                               byte[] bytes = new byte[thestring.Length];
                               for (int ii = 0; ii < thestring.Length; ii++)
                               {
                                   bytes[ii] = (byte)thestring[ii];
                               }
                               XmlizedString = UnicodeByteArrayToString(bytes);
                               return XmlizedString.Replace("utf-8", "utf-16");
                           }
                           //using (XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.Unicode))
                           //{
                           //    xs.Serialize(xmlTextWriter, data);

                           //    string thestring = xmlTextWriter.ToString();
                           //    byte[] bytes = new byte[thestring.Length];
                           //    for (int ii = 0; ii < thestring.Length; ii++)
                           //    {
                           //        bytes[ii] = (byte)thestring[ii];
                           //    }
                           //    XmlizedString = UnicodeByteArrayToString(bytes);
                           //    return XmlizedString.Replace("utf-8", "utf-16");
                           //}
                       }
                   }
                   catch (Exception e)
                   {
                       Debug.WriteLine(e.Message);
                   }
               }
           }
           return string.Empty;
       }

       private static string UnicodeByteArrayToString(byte[] characters)
       {
           System.Text.UnicodeEncoding encoding = new System.Text.UnicodeEncoding();
           string constructedString = encoding.GetString(characters, 0, characters.Length);
           return (constructedString);
       }
    }
}
